package com.gy.controller;

import com.gy.pojo.BaoXiu;
import com.gy.pojo.LeiXing;
import com.gy.pojo.QuYu;
import com.gy.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
public class BaoXiuController {
    @Autowired
    BaoXiuService baoXiuService;
    @Autowired
    LeiXingService leiXingService;
    @Autowired
    NoticeService noticeService;
    @Autowired
    UserService userService;
    @Autowired
    QuYuService quYuService;

    //查看所有报修
    @GetMapping("/baoxiu/selectbaoxiu")
    public String selectBX(Model model){
        List<BaoXiu> baoXius = baoXiuService.selectBX();
        model.addAttribute("selectbx",baoXius);


        return "baoxiu/list";
    }
    //查看我的报修
    //{username}是前端传过来的用户名 ${session.loginUser}  使用@PathVariable("username")接受
    @RequestMapping("/baoxiu/mybaoxiu/{username}")
    public String selectBXByName(@PathVariable("username") String uaername ,Model model){

        List<BaoXiu> baoXius = baoXiuService.selectBXById(uaername);
        model.addAttribute("mybx",baoXius);

        return "baoxiu/mybaoxiu";
    }

    //来到增加报修页面
    @RequestMapping("/baoxiu/addbaoxiu")
    public String addBX(Model model){
        List<LeiXing> leiXings = leiXingService.selectLX();
        model.addAttribute("leixing",leiXings);
        List<QuYu> quYus = quYuService.selectQY();
        //去重
        List<String> qn = quYuService.disQname();
        List<String> ql = quYuService.disQlou();
        List<Integer> qf = quYuService.disQfang();
        model.addAttribute("qname",qn);
        model.addAttribute("qlou",ql);
        model.addAttribute("qfang",qf);
        model.addAttribute("quyu",quYus);

        return "baoxiu/addbaoxiu";
    }

    //增加报修
    @PostMapping("/baoxiu/addbaoxiu2")
    public String addBX2(Integer Bno,String Bname,Integer Lid,String Qname,String Qlou,Integer Qfang,String Xiangxi){
        //先查出来Qid
        int Qid = quYuService.selectID(Qname, Qlou, Qfang);
        baoXiuService.addBX(Bno,Bname,Lid,Qid,Xiangxi);
        return "redirect:/main";
    }

    //取消保修
    @RequestMapping("/baoxiu/deletebaoxiu/{Bid}")
    public String deleteBX(@PathVariable("Bid") int Bid){
        baoXiuService.deleteBXById(Bid);
        return "redirect:/main";
    }


    //处理报修
        //来到页面
    @RequestMapping("/worker/updatebaoxiu")
    public String updateBX(Model model){
        List<BaoXiu> baoXius = baoXiuService.selectBX();
        model.addAttribute("baoxiu",baoXius);
        return "worker/workerlist";
    }
        //进入详细报修页面
    @RequestMapping("/worker/completebaoxiu/{Bid}")
    public String selectbaoxiu(@PathVariable("Bid") int Bid){
        String Jindu="已处理";
        baoXiuService.upsetBX(Bid,Jindu);


        return "redirect:/worker/updatebaoxiu";
    }










}
